# 通过Xpath来筛选网易云音乐中音乐列表中的音乐信息并通过下载链接下载
import requests
from lxml import etree
from fake_useragent import FakeUserAgent
import os

if not os.path.exists('网易云'):
    os.makedirs('网易云')
# 定义音乐列表网址
url = 'https://music.163.com/playlist?id=2430524968'

# 定义请求头信息
headers = {'User-Agent': FakeUserAgent().random,
           "cookie": "NMTID=00OmtGWWERqXGJJhENgl_mDO_EI3T8AAAGT1SDWVA; _iuqxldmzr_=32; _ntes_nnid=da0044685f312142c5f5dbf36eb5bffc,1734447521093; _ntes_nuid=da0044685f312142c5f5dbf36eb5bffc; WEVNSM=1.0.0; WNMCID=dugfib.1734447521377.01.0; WM_TID=AwKJNQhb2h5BQFFRQQLHH6cyTQWm9E%2Bn; __snaker__id=riAHBtANCuJLzRDH; sDeviceId=YD-JqXxdFnGYK1BAkEERUKXD7NjXRX8Uc2g; ntes_utid=tid._.l1twKiPKuIVFBwREVQaXDqNnCBH5FNmK._.0; MUSIC_U=00EC6ACABA2DA8DD26A5D818ACA0B171DED9BF2C85B7EC9892D445231DE9F3E243F2B2B4E2AB39CBE551FCD92B5E27B637818EEA71D162EE4296B6C2759A7247C182728D876EEA6C5A4F508AC8CC0F933D6BB3E0FDE4349B96283A5A30D4C48DF617816748D3C8DC284E105422D9656684AF763A1D7E5B63C3489AAA5F1FE3148A452724246E2CECBD949F70D403088280C38CBFB64B573F9CBF13A60D2040EED93821E051B2C667E4901C206C12AFCFD0A95E003EB26AC6E76D3ED81AD5C12CFED31D97C637290D8A91059AE15003D8E7900B5BD7A6DC6645E430253945E4E9E20C5139ED9348958CA1AD2BF92E231D74A4C8AAB836F1BCE846DF5C729A130DF47F3A25A516595A44A28040A89B331F88D3D6EFFBA7FBA0174D74838B92EC0183BF7DCF07F3DDC5265591F8A2F0388C23FA51538EDCD4FF38E2D5B2DE745C98097E9652B7ABAEDB5A405EF45E86BBD926574D39EFAA6AD32D4D21C47BB2DE25B1; ntes_kaola_ad=1; __csrf=40208b940740e38b995dcf3a749ccfd2; __csrf=40208b940740e38b995dcf3a749ccfd2; JSESSIONID-WYYY=sqc4OF2Cq4KjMaQ%5C2KHPPuzm5%2FpxgR9iOFPa0sqmT6Z%2FvqBiifYlX9zVC9x5e5I9Wl%5CGQTFCEVxdhgdS2ao50SylXjgOuTlzZgT8CpTmVRsX3f0%2B7kzonl4o6sxXSFuhtvOvIzZMgeJgvTYGmeeZx%5C%2FkJE7XrxnxsH0kK2%2B65GQywsR7%3A1745941311767; WM_NI=FcQ2iDAt1dRRu%2FPTLicaQyXFj31c8qt6%2BqdNzI0AJpXHevFtj23hZwMqBeTuk%2F5%2FiHpYPJxP8yGViaZ9AC%2FDurG8Iq4pqqGHoLBhafqlqEwdlPW1FgkMzxzfEkfT2DSSeEQ%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee91ee3db0eef982c839f5ef8ea7d54e978b8eb1db5c9b8bacb2f239f49bab98ef2af0fea7c3b92a86bd9fd3ec5083acff85d15ab7f1a0b5cf63a1bf9f9adb4eaaedff84bb7bb8eae592e746ba9700a2c97c9c98ab97c580aaeefd89d97d8faf8e86d77fa9f1fbd0ec44b487a0abfb5cfca789b7ec6b9ae78fa4ae50b7ae89d9cc7bf8b0bf92c644ad948e89e46eb3b4bfa6f670b09099d2c56da993b699f96e87b5aab5b150918daba6d437e2a3"
           }

# 发起网络请求
response = requests.get(url, headers=headers)

# 进行筛选得到音乐名，音乐id
tree = etree.HTML(response.text)
music_name_result = tree.xpath('//ul[@class="f-hide"]/li/a/text()')
music_id_result = tree.xpath('//ul[@class="f-hide"]/li/a/@href')

# 筛选出id后进行切割得到所需信息
music_id_list = []
for music_id in music_id_result:
    music_id = music_id.split('g')[1]
    music_id_list.append(music_id)

# 通过for循环来进行音乐下载
for music_id, music_name in zip(music_id_list, music_name_result):
    music_url = 'https://music.163.com/song/media/outer/url'+music_id
    response = requests.get(music_url)
    with open('网易云/'+music_name+'.mp3', 'wb') as f:
        f.write(response.content)
    print('音乐'+music_name+'下载完成')
